﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using Bll;
using System.Data;
using System.Security.Cryptography;

namespace 财会系.Admin
{
    public partial class AdminRevise : System.Web.UI.Page
    {
        BllAdmin b = new BllAdmin();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Type"] != null)
                {
                    Grade_Name();
                    datashow();
                }
                
            }
        }
        public void Grade_Name()
        {
            BllBackground bbg = new BllBackground();
            DataTable dt = bbg.Nav_Name_All();

            foreach (DataRow my in dt.Rows)
            {
                if (my["Id"].ToString() == "2")
                {
                    Grade_Name1.InnerHtml = my["Name"].ToString();
                    Check_Geade1.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "3")
                {
                    Grade_Name2.InnerHtml = my["Name"].ToString();
                    Check_Geade2.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "4")
                {
                    Grade_Name3.InnerHtml = my["Name"].ToString();
                    Check_Geade3.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "5")
                {
                    Grade_Name4.InnerHtml = my["Name"].ToString();
                    Check_Geade4.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "6")
                {
                    Grade_Name5.InnerHtml = my["Name"].ToString();
                    Check_Geade5.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "7")
                {
                    Grade_Name6.InnerHtml = my["Name"].ToString();
                    Check_Geade6.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "8")
                {
                    Grade_Name7.InnerHtml = my["Name"].ToString();
                    Check_Geade7.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "9")
                {
                    Grade_Name8.InnerHtml = my["Name"].ToString();
                    Check_Geade8.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "10")
                {
                    Grade_Name9.InnerHtml = my["Name"].ToString();
                    Check_Geade9.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "11")
                {
                    Grade_Name10.InnerHtml = my["Name"].ToString();
                    Check_Geade10.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "12")
                {
                    Grade_Name11.InnerHtml = my["Name"].ToString();
                    Check_Geade11.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "13")
                {
                    Grade_Name12.InnerHtml = my["Name"].ToString();
                    Check_Geade12.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "14")
                {
                    Grade_Name13.InnerHtml = my["Name"].ToString();
                    Check_Geade13.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "15")
                {
                    Grade_Name14.InnerHtml = my["Name"].ToString();
                    Check_Geade14.Value = my["Id"].ToString();
                }
                if (my["Id"].ToString() == "17")
                {
                    Grade_Name15.InnerHtml = my["Name"].ToString();
                    Check_Geade15.Value = my["Id"].ToString();
                }
            }


        }

        public void datashow()
        {
            if (Request.QueryString["id"] != null)
            {
                ModelAdmin m = new ModelAdmin();
                m.AdminId = int.Parse(Request.QueryString["id"].ToString());

                DataTable dt = b.Singlex_Information(m);

                foreach (DataRow my in dt.Rows)
                {
                    txtAcc.Text = my["Acc"].ToString();
                    txtName.Text = my["Name"].ToString();
                    lblPwd.Text = my["Pwd"].ToString();
                    if (my["Grade"].ToString() == "1" && my["Type"].ToString() == "0")
                    {
                        Grade_Super.Checked = true;
                        Grade_visible(1);
                    }
                    if (my["Grade"].ToString() != "1" && my["Type"].ToString() == "0")
                    {
                        Grade_Teach.Checked = true;
                        Grade_visible(2);

                        string grade = my["Grade"].ToString();
                        string[] arr_Grade = grade.Split(',');

                        for (int i = 0; i < arr_Grade.Length; i++)
                        {
                            Check_Grade(arr_Grade[i]);
                        }

                    }
                    if (my["Grade"].ToString() != "1" && my["Type"].ToString() == "1")
                    {
                        Grade_Student.Checked = true;
                        Grade_visible(2);

                        string grade = my["Grade"].ToString();
                        string[] arr_Grade = grade.Split(',');

                        for (int i = 0; i < arr_Grade.Length; i++)
                        {
                            Check_Grade(arr_Grade[i]);
                        }

                    }
                }
            }
            else
            {
                Response.Redirect("adminAdmin.aspx");
            }
        }

        private void Check_Grade(string id)
        {
            if (id == "2")
            {
                Check_Geade1.Checked = true;
            }
            if (id == "3")
            {
                Check_Geade2.Checked = true;
            }
            if (id == "4")
            {
                Check_Geade3.Checked = true;
            }
            if (id == "5")
            {
                Check_Geade4.Checked = true;
            }
            if (id == "6")
            {
                Check_Geade5.Checked = true;
            }
            if (id == "7")
            {
                Check_Geade6.Checked = true;
            }
            if (id == "8")
            {
                Check_Geade7.Checked = true;
            }
            if (id == "9")
            {
                Check_Geade8.Checked = true;
            }
            if (id == "10")
            {
                Check_Geade9.Checked = true;
            }
            if (id == "11")
            {
                Check_Geade10.Checked = true;
            }
            if (id == "12")
            {
                Check_Geade11.Checked = true;
            }
            if (id == "13")
            {
                Check_Geade12.Checked = true;
            }
            if (id == "14")
            {
                Check_Geade13.Checked = true;
            }
            if (id == "15")
            {
                Check_Geade14.Checked = true;
            }
        }

        protected void btnAdminSave_Click(object sender, EventArgs e)
        {
            ModelAdmin m = new ModelAdmin();
            m.Acc = txtAcc.Text.Trim();
            if (txtPwd.Text.Trim() == "")
            {
                m.Pwd = lblPwd.Text;
                m.PwdType = 0;
            }
            else
            {
                m.Pwd = txtPwd.Text.Trim();
                m.PwdType = 1;
            }

            m.AdminId = int.Parse(Request.QueryString["id"].ToString());
            m.Name = txtName.Text.Trim();
            if (Grade_Super.Checked == true)
            {
                m.Grade = "1";
                m.Type = 0;
            }
            if (Grade_Teach.Checked == true)
            {
                m.Grade = Grade_Num();
                m.Type = 0;
            }
            if (Grade_Student.Checked == true)
            {
                m.Grade = Grade_Num();
                m.Type = 1;
            }

            if (b.Update_Information(m))
            {
                Response.Redirect("adminAdmin.aspx");
            }
            else
            {
                Response.Write("<script>confirm('更改失败！')?window.loaction='adminRevise.aspx':window.loaction='adminRevise.aspx'</script>");
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("adminAdmin.aspx");
        }

        private string Grade_Num()
        {
            string strGrade = "";

            if (Check_Geade1.Checked)
                strGrade += Check_Geade1.Value + ",";
            if (Check_Geade2.Checked)
                strGrade += Check_Geade2.Value + ",";
            if (Check_Geade3.Checked)
                strGrade += Check_Geade3.Value + ",";
            if (Check_Geade4.Checked)
                strGrade += Check_Geade4.Value + ",";
            if (Check_Geade5.Checked)
                strGrade += Check_Geade5.Value + ",";
            if (Check_Geade6.Checked)
                strGrade += Check_Geade6.Value + ",";
            if (Check_Geade7.Checked)
                strGrade += Check_Geade7.Value + ",";
            if (Check_Geade8.Checked)
                strGrade += Check_Geade8.Value + ",";
            if (Check_Geade9.Checked)
                strGrade += Check_Geade9.Value + ",";
            if (Check_Geade10.Checked)
                strGrade += Check_Geade10.Value + ",";
            if (Check_Geade11.Checked)
                strGrade += Check_Geade11.Value + ",";
            if (Check_Geade12.Checked)
                strGrade += Check_Geade12.Value + ",";
            if (Check_Geade13.Checked)
                strGrade += Check_Geade13.Value + ",";
            if (Check_Geade14.Checked)
                strGrade += Check_Geade14.Value + ",";
            if (Check_Geade15.Checked)
                strGrade += Check_Geade15.Value;

            return strGrade;
        }

        private void Grade_visible(int i)
        {
            if(i==1)
            {

                Check_Geade1.Disabled = true;
                Check_Geade2.Disabled = true;
                Check_Geade3.Disabled = true;
                Check_Geade4.Disabled = true;
                Check_Geade5.Disabled = true;
                Check_Geade6.Disabled = true;
                Check_Geade7.Disabled = true;
                Check_Geade8.Disabled = true;
                Check_Geade9.Disabled = true;
                Check_Geade10.Disabled = true;
                Check_Geade11.Disabled = true;
                Check_Geade12.Disabled = true;
                Check_Geade13.Disabled = true;
                Check_Geade14.Disabled = true;
                Check_Geade15.Disabled = true;
                
            }
            else
            {
                Check_Geade1.Disabled = false;
                Check_Geade2.Disabled = false;
                Check_Geade3.Disabled = false;
                Check_Geade4.Disabled = false;
                Check_Geade5.Disabled = false;
                Check_Geade6.Disabled = false;
                Check_Geade7.Disabled = false;
                Check_Geade8.Disabled = false;
                Check_Geade9.Disabled = false;
                Check_Geade10.Disabled = false;
                Check_Geade11.Disabled = false;
                Check_Geade12.Disabled = false;
                Check_Geade13.Disabled = false;
                Check_Geade14.Disabled = false;
                Check_Geade15.Disabled = false;
            }
        }
    }
}